home *** CD-ROM | disk | FTP | other *** search
/ NeXT Education Software Sampler 1992 Fall / NeXT Education Software Sampler 1992 Fall.iso / Programming / Source / HippoDraw / HippoDrawSrc1.1 / Hippo.subproj / GridView.m < prev    next >
Encoding:
Text File  |  1992-04-25  |  4.9 KB  |  249 lines

  1. #import "GridView.h"
  2. #import "GraphicView.h"
  3. #import <appkit/Application.h>
  4. #import <appkit/Button.h>
  5. #import <appkit/Window.h>
  6. #import <appkit/nextstd.h>
  7. #import <dpsclient/wraps.h>
  8. #import <dpsclient/dpsclient.h>
  9.  
  10. @implementation GridView
  11. /*
  12.  * This class is the heart of the Grid Inspector modal panel.
  13.  * It implements the draggable grid.  It also provides the external
  14.  * interface (i.e. the runModalForGraphicView: method) to running
  15.  * the panel.  It is a good example of a modal panel.
  16.  * See the Interface Builder file for a better understanding of
  17.  * the outlets and actions sent by controls in the window containing
  18.  * the GridView.
  19.  */
  20.  
  21. - runModalForGraphicView:view
  22. {
  23.     int gridSpacing;
  24.     float gridGray;
  25.  
  26.     if (graphicView != view) {
  27.     graphicView = view;
  28.     gridSpacing = [view gridSpacing];
  29.     [spacing setIntValue:(gridSpacing >= 4 ? gridSpacing : 10)];
  30.     gridGray = [view gridGray];
  31.     [grayField setFloatValue:gridGray];
  32.     [graySlider setFloatValue:gridGray];
  33.     [self display];
  34.     }
  35.  
  36.     [NXApp runModalFor:window];
  37.     [window orderOut:self];
  38.  
  39.     return self;
  40. }
  41.  
  42. - drawGrid:(int)grid
  43. {
  44.     int numXLines, numYLines, maxLines, i, ci, oi;
  45.     float *cArray;
  46.     char *oArray;
  47.     float bbox[4];
  48.     float x, y, x0, y0, x1, y1, increment;     
  49.     increment = grid;
  50.     numXLines = bounds.size.width / increment;
  51.     numYLines = bounds.size.height / increment;
  52.     maxLines = (numXLines > numYLines) ? numXLines : numYLines;
  53.     
  54.     NX_ZONEMALLOC( [self zone], cArray, float, (4*maxLines) );
  55.     if ( cArray == NULL)
  56.     {
  57.         fprintf(stderr,"drawGrid - memory exceeded \n");
  58.         return self;
  59.     }
  60.      
  61.     NX_ZONEMALLOC( [self zone], oArray, char, (2*maxLines) );
  62.     if ( oArray== NULL)
  63.     {
  64.         fprintf(stderr,"drawGrid - memory exceeded \n");
  65.         return self;
  66.     }
  67.      
  68.     x = bounds.origin.x;
  69.     y0 = bounds.origin.y;
  70.     y1 = y0 + bounds.size.height;
  71.     ci = oi = 0;
  72.     
  73.     /* fill bbox */
  74.     bbox[0] = x;
  75.     bbox[1] = y0;
  76.     bbox[2] = x + bounds.size.width;
  77.     bbox[3] = y1;
  78.      
  79.     for (i = 0; i < numXLines; i++)
  80.     {
  81.         cArray[ci++] = x;
  82.         cArray[ci++] = y0;
  83.         oArray[oi++] = dps_moveto;
  84.      
  85.         cArray[ci++] = x;
  86.         cArray[ci++] = y1;
  87.         oArray[oi++] = dps_lineto;
  88.         
  89.         x += increment;
  90.     }
  91.     DPSDoUserPath (cArray, ci, dps_float, oArray, oi, bbox, dps_ustroke); 
  92.  
  93.     y = bounds.origin.y;
  94.     x0 = bounds.origin.x;
  95.     x1 = x0 + bounds.size.width;
  96.     ci = oi = 0;
  97.     
  98.     /* fill bbox */
  99.     bbox[0] = x0;
  100.     bbox[1] = y;
  101.     bbox[2] = x1;
  102.     bbox[3] = y + bounds.size.height;
  103.      
  104.     for (i = 0; i < numYLines; i++)
  105.     {
  106.         cArray[ci++] = x0;
  107.         cArray[ci++] = y;
  108.         oArray[oi++] = dps_moveto;
  109.      
  110.         cArray[ci++] = x1;
  111.         cArray[ci++] = y;
  112.         oArray[oi++] = dps_lineto;
  113.         
  114.         y += increment;
  115.     }
  116.     DPSDoUserPath (cArray, ci, dps_float, oArray, oi, bbox, dps_ustroke); 
  117.  
  118.     NX_FREE(cArray);
  119.     NX_FREE(oArray);
  120.     return self;
  121. }
  122.  
  123. - drawSelf:(const NXRect *)rects :(int)rectCount
  124. {
  125.     int grid;
  126.     float gray;
  127.  
  128.     grid = [spacing intValue];
  129.     grid = MAX(grid, 0.0);
  130.     PSsetgray(NX_WHITE);
  131.     NXRectFillList(rects, rectCount);
  132.     if (grid >= 4) {
  133.     gray = [grayField floatValue];
  134.     gray = MIN(gray, 1.0);
  135.     gray = MAX(gray, 0.0);
  136.     PSsetgray(gray);
  137.     PSsetlinewidth(0.0);
  138.     [self drawGrid:grid];
  139.     }
  140.     PSsetgray(NX_BLACK);
  141.     NXFrameRect(&bounds);
  142.  
  143.     return self;
  144. }
  145.  
  146. - mouseDown:(NXEvent *)event
  147. {
  148.     NXPoint p, start;
  149.     int grid, gridCount;
  150.  
  151.     start = event->location;
  152.     [self convertPoint:&start fromView:nil];
  153.     grid = MAX([spacing intValue], 1.0);
  154.     gridCount = (int)MAX(start.x, start.y) / grid;
  155.     gridCount = MAX(gridCount, 1.0);
  156.  
  157.     [window addToEventMask:NX_MOUSEDRAGGEDMASK|NX_MOUSEUPMASK];
  158.  
  159.     event = [NXApp getNextEvent:NX_MOUSEDRAGGEDMASK|NX_MOUSEUPMASK];
  160.     while (event->type != NX_MOUSEUP) {
  161.     p = event->location;
  162.     [self convertPoint:&p fromView:nil];
  163.     grid = (int)MAX(p.x, p.y) / gridCount;
  164.     grid = MAX(grid, 1.0);
  165.     if (grid != [spacing intValue]) {
  166.         [form abortEditing];
  167.         [spacing setIntValue:grid];
  168.         [self display];
  169.     }
  170.     event = [NXApp getNextEvent:NX_MOUSEDRAGGEDMASK|NX_MOUSEUPMASK];
  171.     }
  172.  
  173.     return self;
  174. }
  175.  
  176. /* Target/Action methods */
  177.  
  178. - show:sender
  179. {
  180.     [NXApp stopModal];
  181.     [graphicView setGridSpacing:[spacing intValue]
  182.     andGray:[grayField floatValue]];
  183.     [graphicView setGridVisible:YES];
  184.     return self; 
  185. }
  186.  
  187. - off:sender
  188. {
  189.     [NXApp stopModal];
  190.     [graphicView setGridSpacing:1];
  191.     return self;
  192. }
  193.  
  194. - cancel:sender
  195. {
  196.     [NXApp stopModal];
  197.     return self;
  198. }
  199.  
  200. - changeSpacing:sender
  201. {
  202.     return [self display];
  203. }
  204.  
  205. - changeGray:sender
  206. {
  207.     if (sender == graySlider) {
  208.     [form abortEditing];
  209.     [grayField setFloatValue:[sender floatValue]];
  210.     } else {
  211.     [graySlider setFloatValue:[sender floatValue]];
  212.     }
  213.     return [self display];
  214. }
  215.  
  216. /* NIB outlet setting methods */
  217.  
  218. - setSpacing:anObject
  219. {
  220.     spacing = anObject;
  221.     return self;
  222. }
  223.  
  224. - setGrayField:anObject
  225. {
  226.     grayField = anObject;
  227.     return self;
  228. }
  229.  
  230. - setGraySlider:anObject
  231. {
  232.     graySlider = anObject;
  233.     return self;
  234. }
  235.  
  236. - setAppIconButton:anObject
  237. {
  238.     [anObject setIcon:"appIcon"];
  239.     return self;
  240. }
  241.  
  242. - setForm:anObject
  243. {
  244.     form = anObject;
  245.     return self;
  246. }
  247.  
  248. @end
  249.